package org.springblade.modules.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.*;
import lombok.AllArgsConstructor;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.mp.base.BaseEntity;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.node.ForestNodeMerger;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.admin.dto.parts.BatchRelationCategoryDTO;
import org.springblade.modules.admin.entity.MaintainParts;
import org.springblade.modules.admin.service.MaintainPartsService;
import org.springblade.modules.admin.vo.MaintainPartsTreeVO;
import org.springblade.modules.admin.vo.MaintainPartsVO;
import org.springblade.modules.admin.vo.MaintainPartsWithAttributesTreeVO;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@AllArgsConstructor
@RequestMapping("admin/parts")
@Api(value = "配件分类表", tags = "配件分类接口")
public class MaintainPartsController {

	private final MaintainPartsService maintainPartsService;

	@GetMapping("/list")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "列表")
	public R<List<MaintainPartsVO>> list(MaintainParts maintainParts) {
		List<MaintainPartsVO> list = maintainPartsService.partsList(maintainParts);
		return R.data(list);
	}

	@GetMapping("/tree")
	@ApiOperationSupport(order = 31)
	@ApiOperation(value = "树形列表")
	public R<List<MaintainPartsTreeVO>> tree(MaintainParts maintainParts) {
		List<MaintainPartsTreeVO> list = maintainPartsService.partsTree(maintainParts);
		return R.data(ForestNodeMerger.merge(list));
	}

	@GetMapping("/tree/with-attributes")
	@ApiOperationSupport(order = 32)
	@ApiOperation(value = "树形列表带属性")
	public R<List<MaintainPartsWithAttributesTreeVO>> treeWithAttributes(MaintainParts maintainParts) {
		List<MaintainPartsWithAttributesTreeVO> resultList = maintainPartsService.partsTreeWithAttributes(maintainParts);
		return R.data(ForestNodeMerger.merge(resultList));
	}

	/**
	 * 删除配件
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 21)
	@ApiOperation(value = "逻辑删除")
	public R<Boolean> remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		if(isNotAdminTenant()){
			throw new ServiceException("请在000000租户执行此操作");
		}
		List<Long> partsIdList = Func.toLongList(ids);
		partsIdList.forEach(p -> {
			List<MaintainParts> childList = maintainPartsService.list(new LambdaQueryWrapper<MaintainParts>()
				.eq(MaintainParts::getParentId, p)
				.eq(BaseEntity::getIsDeleted, 0)
			);
			if(!Func.isEmpty(childList)){
				throw new ServiceException("该配件下存在子配件，请先删除子配件");
			}
		});
		return R.status(maintainPartsService.removePartsByIds(partsIdList));
	}



	/**
	 * 新增或编辑
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或编辑")
	public R submit(@Valid @RequestBody MaintainParts maintainParts) {
		return R.status(maintainPartsService.submit(maintainParts));
	}

	@PostMapping("/batch/relation/category")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "批量关联商品类目")
	public R batchRelationCategory(@Valid @RequestBody BatchRelationCategoryDTO param) {
		return R.status(maintainPartsService.batchRelationCategory(param));
	}

	@GetMapping("/getElevatorStructureTree")
	@ApiOperationSupport(order = 8)
	@ApiOperation(value = "获取电梯结构模板关联配件树")
	public R<List<MaintainPartsTreeVO>> getElevatorStructureTree(@ApiParam(value = "电梯id") @RequestParam Long elevatorId) {
		List<MaintainPartsTreeVO> list = maintainPartsService.getElevatorStructureTree(elevatorId);
		return R.data(ForestNodeMerger.merge(list));
	}

	@PostMapping("/initData")
	@ApiOperation(value = "处理历史数据脚本")
	public R initData() {
		return R.status(maintainPartsService.initData());
	}

	private boolean isNotAdminTenant() {
		String adminTenantId = AuthUtil.getTenantId();
		return !"000000".equals(adminTenantId);
	}
}
